package com.xph.core.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;
import com.xph.core.common.util.JsonUtil;
import com.xph.core.common.util.RestUtil;
import com.xph.core.service.impl.XphService;

@Path("/xph") 
@Api(value = "/xph", description = "商品相关")
public class XphRest
{
    private static Logger log = Logger.getLogger(XphRest.class);
    
    XphService xphServ;

	/**
     * 分页获取商品列表
     * @param source    来源
     * @return
     * @throws Exception
     */
    @GET
    @Path("/getdoodslst/{source}")
    @Produces(MediaType.APPLICATION_JSON) 
    @ApiOperation(value = "分页获取商品列表")
    @ApiResponses(value = { @ApiResponse(code = 9999, message = "failed")})
    public JSONObject getxphPagenate(
    		@ApiParam(value = "source", required = true, defaultValue = "1") @PathParam("source") String source
    		,@ApiParam(value = "pageno", required = true, defaultValue = "1") @QueryParam("pageno") String pageno
    		,@ApiParam(value = "limit", required = true, defaultValue = "8") @QueryParam("limit") String limit) throws Exception {
    	Map<String, Object> paraMap = new HashMap<String, Object>();
    	paraMap.put("pageno", pageno);
    	paraMap.put("limit", limit);
    	List<Map<String, Object>> lst = xphServ.getGoodslst(paraMap);
    	JSONArray jsonArray = new JSONArray();
    	JSONObject jsonResult = null;
    	jsonArray = JsonUtil.list2JsonArray(lst);
    	jsonResult = RestUtil.getSuccessResult(jsonArray);
    	
    	// 查询记录总数
        int total = 0;
        paraMap.put("total", "0");
        lst = xphServ.getGoodslst(paraMap);
        total = Integer.parseInt(String.valueOf(((Map<String, Object>) lst.get(0)).get("total")));
        jsonResult.put(RestUtil.RESULT_TOTAL, total); 
    	
    	return jsonResult;
      }


	public void setXphServ(XphService xphServ) {
		this.xphServ = xphServ;
	}
    
}
    

